<template>
	<xn-form-container
		:title="formData.id ? '查看' : '创建ncc单据'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="收支项目：" name="nccAccountType">
				<a-select
					v-model:value="formData.nccAccountType"
					placeholder="请选择收支项目"
					:options="nccAccountTypeOptions"
				/>
			</a-form-item>
			<a-form-item label="收支项目：" name="nccAccountType">
				<a-select v-model:value="formData.nccOrg" placeholder="请选择发生主体" :options="nccOrgOptions" />
			</a-form-item>
			<a-form-item label="收支项目：" name="nccUserCode">
				<a-select v-model:value="formData.nccUserCode" placeholder="请选择提交账号" :options="nccUserCodeOptions" />
			</a-form-item>
			<a-form-item label="物料分类：" name="nccMaterial">
				<a-select v-model:value="formData.nccMaterial" placeholder="请选择物料分类" :options="nccMaterialOptions" />
			</a-form-item>
			<a-form-item label="是否预付：" name="nccAdvancePay">
				<a-select v-model:value="formData.nccAdvancePay" placeholder="请选择是否预付" :options="nccAdvancePayOptions" />
			</a-form-item>
			<a-form-item label="上传ncc单据文件" name="motionFileId">
				<div>
					<a-upload
						list-type="picture"
						v-model:file-list="fileList"
						accept=".pdf, .png,"
						:remove="handleRemove"
						multiple="true"
						:before-upload="beforeUpload"
						class="upload-list-inline"
					>
						<a-button>
							<upload-outlined></upload-outlined>
							选择上传文件
						</a-button>
					</a-upload>
				</div>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">创建</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizAiNccForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import { message } from 'ant-design-vue'
	import bizAiNccApi from '@/api/biz/bizAiNccApi'
	import fileApi from '@/api/biz/bizFileManagerApi.js'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const nccAccountTypeOptions = ref([])
	const nccMaterialOptions = ref([])
	const nccAdvancePayOptions = ref([])
	const nccOrderTypeOptions = ref([])
	const nccOrgOptions = ref([])
	const nccUserCodeOptions = ref([])
	// 上传文件数据
	const fileList = ref([])
	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		nccOrderTypeOptions.value = tool.dictList('NCC_CATE')
		nccAccountTypeOptions.value = tool.dictList('ACCOUNT_CATE')
		nccMaterialOptions.value = tool.dictList('物料分类')
		nccAdvancePayOptions.value = tool.dictList('YSEORNO')
		nccOrgOptions.value = tool.dictList('NCC制单主体')
		nccUserCodeOptions.value = tool.dictList('NCC制单用户名')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		fileList.value = []
		open.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = async () => {
		submitLoading.value = true
		let res = await uploadFile()
		formRef.value
			.validate()
			.then(() => {
				const formDataParam = cloneDeep(formData.value)
				formDataParam.nccFileList = res
				bizAiNccApi
					.bizAiNccSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
						submitLoading.value = false
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	// 文件处理（等选择后手动上传）
	const handleRemove = (file) => {
		const index = fileList.value.indexOf(file)
		const newFileList = fileList.value.slice()
		newFileList.splice(index, 1)
		fileList.value = newFileList
		console.log(newFileList)
	}

	const beforeUpload = (file) => {
		fileList.value = [...fileList.value, file]
		return false
	}

	// 文件上传
	const uploadFile = async () => {
		const formData = new FormData()
		fileList.value.forEach((file) => {
			file = toRaw(file)
			formData.append('files', file.originFileObj)
		})
		return fileApi.fileUploadOssReturnUrl(formData)
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
